<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>活跃粉丝群发</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {include file="public/css"/}
	<style>
	.newspreview{width:312px}
	.newspreview *{box-sizing: border-box;}
	.newscontent{padding:12px;border:1px solid #ddd;}
	.item1{width:100%;cursor:pointer}
	.item1 img{width:100%;height:100%}
	.item1 .title{height:40px;padding:0 10px;width:100%;position:absolute;bottom:0;left:0;color:#fff;background:rgba(100,100,100,0.5);display:flex;align-items:center;word-wrap: break-word;word-break: break-all;overflow:hidden;}
	.item2{width:100%;cursor:pointer;margin-top:5px;padding-top:5px;border-top:1px solid #ddd;display:flex}
	.item2 .title{flex:1;display:flex;align-items:center;}
	.item2 img{width:50px;height:50px}
	</style>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-card layui-col-md12">
				<div class="layui-card-header">
					活跃粉丝群发
					<button class="layui-btn layui-btn-primary layui-btn-sm" onclick="openmax('{:url('kfmsgsendlog')}/isopen/1')" style="float: right;margin-left:10px;margin-top: 6px;">发送记录</button>
				</div>
				<div class="layui-card-body" pad15>

					<blockquote class="layui-elem-quote">
					当用户在48小时之内主动发消息给公众号的时候（包括发送信息、点击自定义菜单(点击推事件、扫码推事件)、关注公众号、扫描二维码、支付成功、用户维权），可以用此功能推送图文消息给用户，互动超过48小时的用户将发送失败
					</blockquote>
					<div class="flex">
						<div class="layui-form flex1 form-label-w6" lay-filter="" style="padding-bottom:20px">
							<div class="layui-form-item">
								<label class="layui-form-label">图文素材：</label>
								<div class="layui-input-inline" style="width:100px">
									<button style="float:left;" type="button" class="layui-btn layui-btn-primary" onclick="selectsource()">选择素材</button>
									<input type="hidden" name="info[media_id]" value="">
								</div>
								<div class="layui-form-mid layui-word-aux" style="color:red">注意：只能选择单图文</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label"></label>
								<div class="layui-input-inline" style="width:300px" id="sourcepreview">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">发送对象：</label>
								<div class="layui-input-inline" style="width:500px">
									<input type="radio" name="info[to_type]" value="1" checked title="填写openid" lay-filter="to_type"/>
									<input type="radio" name="info[to_type]" value="2" title="选择标签" lay-filter="to_type"/>
									<input type="radio" name="info[to_type]" value="3" title="全部用户({$fanscount})" lay-filter="to_type"/>
								</div>
							</div>
							<div class="layui-form-item" id="to_type_1">
								<label class="layui-form-label">用户openid：</label>
								<div class="layui-input-inline" style="width:300px">
									<textarea class="layui-textarea" name="info[openids]" style="height:200px"></textarea>
								</div>
								<div class="layui-form-mid layui-word-aux layui-clear">openid请在粉丝列表中查找，请将用户openid输入或粘贴到文本框中，一行一个，请勿输入一行多个或空行或其他字符</div>
							</div>
							<div class="layui-form-item" id="to_type_2" style="display:none">
								<label class="layui-form-label">选择标签：</label>
								<div class="layui-input-inline" style="width:200px">
									<select name="info[tagid]">
										<option value="">请选择标签</option>
										{foreach $tagslist as $v}
										<option value="{$v.tagid}">{$v.name}({$v.count})</option>
										{/foreach}
									</select>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label"></label>
								<div class="layui-input-block">
									<button class="layui-btn" lay-submit lay-filter="setmypass">确认发送</button>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
    </div>
  </div>
	{include file="public/js"/}
	<script>
	layui.form.on('radio(to_type)',function(data){
		$('#to_type_1').hide();
		$('#to_type_2').hide();
		if(data.value==1){
			$('#to_type_1').show();
		}
		if(data.value==2){
			$('#to_type_2').show();
		}
	})
	var pagenum = 0;
	var pagelimit = 20;
	layui.form.on('submit(setmypass)', function(obj){
		var field = obj.field
		pagenum = 0;
		//var index= layer.load();
		var field = obj.field
		_tplsend(field);
		return;
	})
	function _tplsend(field){
			var index = layer.load();
			pagenum++;
			$.ajax({
				type:'POST',
				url:"{:url('kfmsgsend')}/pagenum/"+pagenum+'/pagelimit/'+pagelimit,
				dataType:'json',
				data:field,
				success:function(res){
					console.log(res)
					layer.close(index);
						layer.msg('已发送'+res.sendcount+'条,成功'+res.successcount+'条,失败'+res.errorcount+'条',{offset:'100px'});
					if(res.status==2){ //继续发送
						field.logid = res.logid
						_tplsend(field);
					}else{
						if(res.status==1){
							dialog('共发送'+res.sendcount+'条,成功'+res.successcount+'条,失败'+res.errorcount+'条',res.status,"{:url('kfmsgsendlog')}");
						}else{
							dialog(res.msg,res.status);
						}
					}
				},
				error:function(){
					layer.close(index);
					dialog('未知错误',0);
				}
			})
	}
  </script>
	<script>
	function selectsource(){
		layer.open({type:2,shadeClose:true,area:['930px', '580px'],'title':'选择素材',content:"{:url('Mpfans/choosesource')}"})
	}
	function choosesource(media_id){
		$.post("{:url('getsourcedata')}",{media_id:media_id},function(res){
			if(res.status==0){
				dialog(res.msg);return;
			}
			var news_item = res.data.news_item
			var nowtime = parseInt(res.time);
			console.log(news_item);
			if(news_item.length>1){
				dialog('只能选择单图文发送');return;
			}
			$("input[name='info[media_id]']").val(media_id)
			html = '';
			html+='<div class="newspreview">';
			html+='	<div class="newscontent">';
			html+='		<div style="height:30px;color:#666">更新时间：'+date('Y-m-d H:i',res.data.update_time)+'</div>';
			html+='		<div id="newscontent_content">';
			for(var i=0;i<news_item.length;i++){
				var item = news_item[i]
				if(i==0){
					html += '<div onclick="changeindex('+i+')" class="item1">';
					html += '	<div style="position:relative;width: 100%;overflow: hidden;height: 160px;">';
					html += '		<img src="'+item.thumb_url+'"/>';
					html += '		<div class="title">'+item.title+'</div>';
					html += '	</div>';
					html += '</div>';
				}else{
					html += '<div onclick="changeindex('+i+')" class="item2">';
					html += '	<div class="title">'+item.title+'</div>';
					html += '	<div><img src="'+item.thumb_url+'"/></div>';
					html += '</div>';
				}
			}
			html+='		</div>';
			html+='	</div>';
			html+='</div>';
			$('#sourcepreview').html(html);
		})
	}
	</script>
	{include file="public/copyright"/}
</body>
</html>